﻿using BBICMS.Infrastructure.UI;
using BBICMS.Model.View;
using BBICMS.Repository;
using BBICMS.WebUI.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    [SIGAuth]
    
    public class AdController : Controller
    {
        public int PageSize = 10;
        private ApplicationUnit _unit = new ApplicationUnit();
        //
        // GET: /bbi_Admin/Ad/

       
        public ActionResult AdList(int page = 1)
        {
            AdListVM vm = new AdListVM
            {
                Advertisements = _unit.Advertisements.GetAll().OrderBy(p => p.Importance)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = _unit.Advertisements.GetAll().Count()
                }
            };
            return View(vm);
        }

          
           public ViewResult EditAd(int Id)
           {
               AdVM avm = new AdVM();
               avm.Ad = _unit.Advertisements.GetById(Id);
               avm.Positions = _unit.Positions.GetAll().ToList();

               if (avm.Ad != null)
               {
                   return View(avm);
               }
               return View("NotFound");
           }


           //  [ActionName("EditAd")]
          
           public ActionResult CreateAd()
           {
               AdVM avm = new AdVM();
               var vAd = new BBICMS.Model.Advertisement();
               vAd.Importance = 0;
               avm.Ad = vAd;
               avm.Positions = _unit.Positions.GetAll().ToList();

               return View("EditAd", avm);

           }

       
        public ActionResult PositionList()
        {
            PositionListVM vm = new PositionListVM();

            var lPositions = _unit.Positions.GetAll();

            if (lPositions != null)
            {
                vm.Positions = lPositions.ToList();
                return View(vm);
            }


            return View("NotFound");
        }

       
        public ActionResult CreatePosition()
        {
            var vPosition = new BBICMS.Model.Position();
            vPosition.Importance = 0;
            vPosition.Active = true;

            return View("EditPosition", vPosition);

        }

       
        public ActionResult EditPosition(int Id)
        {
            var vPosition = _unit.Positions.GetById(Id);
            if (vPosition != null)
            {
                return View(vPosition);
            }
            return View("NotFound");
        }

    }
}
